﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace eeenno.ReaderEngine.Objects
{
    [Serializable]

    /// <summary>
    /// Google Reader tag object
    /// </summary>
    public class Tag
    {
        private string _name;
        private int _index;
        private string _id;
        private int _unread_count;
        private bool _google_tag;
        private bool _articles_loaded = false;

        public Subscriptions _subscriptions = null;
        public Articles _articles = null;

        public Tag()
        {
            _subscriptions = new Subscriptions();
        }

        public Tag(string name, int unread_count)
        {
            _name = name;
            _unread_count = unread_count;
        }

        public Tag(string name, string id)
        {
            _name = name;
            _id = id;
        }

        public bool articles_loaded
        {
            get { return _articles_loaded; }
            set { _articles_loaded = value; }
        }

        public string name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int unread_count
        {
            get { return _unread_count; }
        }

        public int article_count
        {
            get { return _subscriptions.article_count; }
        }

        public Subscriptions subscriptions
        {
            get { return _subscriptions; }
            set { _subscriptions = value; }
        }

        public Articles articles
        {
            get { return GetArticles(); }
        }

        public int index
        {
            get { return _index; }
            set { _index = value; }
        }

        public bool google_tag
        {
            get { return _google_tag; }
            set { _google_tag = value; }
        }

        public string id
        {
            get { return _id; }
            set { _id = value; }
        }

        public void RefreshUnreadCount()
        {
            _unread_count = 0;
            for (int i = 0; i < _subscriptions.Count; i++)
            {
                //so we don't count the unreads in each tags reading list
                if (!_subscriptions.Get(i).is_reading_list)
                {
                    _unread_count += _subscriptions.Get(i).unread_count;
                }
            }
        }

        /// <summary>
        /// Used for Google Reader tag items
        /// </summary>
        /// <returns></returns>
        private Articles GetArticles()
        {
            if (_articles == null)
            {
                _articles = SpeeedReader.GetArticlesByState(_name);
                articles_loaded = true;
                return _articles;
            }
            else
            {
                return _articles;
            }
        }
    }
}
